package com.zz.client.handle.defaults;

import com.zz.common.annotation.Executor;
import com.zz.common.config.Constants;
import com.zz.common.entity.Information;
import com.zz.common.entity.Meta;
import com.zz.common.entity.ProtocolInfo;
import com.zz.common.enums.MessageType;
import com.zz.common.enums.SerializationType;
import com.zz.common.handle.executor.AbstractMessageExecutor;
import com.zz.common.protocol.DefaultProtocol;
import com.zz.common.utils.ObjectUtil;
import com.zz.common.utils.SnowflakeUtil;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.internal.EmptyArrays;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;

/**
 * 数据转发
 */
@Slf4j
@Executor(msgType = MessageType.TYPE_TRANSFER)
public class TransferExecutor extends AbstractMessageExecutor {

    @Override
    public void execute(Information information) {
        ChannelHandlerContext context = information.getContext();
        DefaultProtocol protocol = (DefaultProtocol) information.getProtocolMsg();
        Channel localChannel = context.channel().attr(Constants.Attributes.LOCAL_CHANNEL).get();
        if (ObjectUtil.isNull(localChannel)) {
            log.error("local channel is null");
            return;
        }
        localChannel.writeAndFlush(Optional.ofNullable(protocol.getData()).orElse(EmptyArrays.EMPTY_BYTES));
    }

    @Override
    public void execute(ProtocolInfo protocolInfo) {
        ChannelHandlerContext context = protocolInfo.getContext();
        Meta meta = context.channel().attr(Constants.Attributes.META).get();
        if (ObjectUtil.isNull(protocolInfo.getServerChannel()) || ObjectUtil.isNull(meta)) {
            log.error("salve or meta channel is null");
            return;
        }
        DefaultProtocol protocol = DefaultProtocol.builder().type(protocolInfo.getType())
                .serialNumber(SnowflakeUtil.genSnowflakeId()).serializationType(SerializationType.PROTOSTUFF)
                .data(protocolInfo.getData()).meta(meta).build();
        protocolInfo.getServerChannel().writeAndFlush(protocol);

    }


}
